function sp = get_sp2(temp_m, islog)


% input: temp_m, islog,
ncity = size(temp_m.alp_j,1);
if ~isfield(temp_m, 'del_j')
    temp_m.del_j = zeros(ncity,1);
end
if ~isfield(temp_m, 'del2_j')
    temp_m.del2_j = zeros(ncity,1);
end
if ~isfield(temp_m, 'del3_j')
    temp_m.del3_j = zeros(ncity,1);
end
if ~isfield(temp_m, 'del4_j')
    temp_m.del4_j = zeros(ncity,1);
end

% t=1
if islog == 1
    sp.spfun = @(xgrid) repmat(temp_m.alp_j',length(xgrid),1) + log(xgrid)*temp_m.del_j' + log(xgrid).^2*temp_m.del2_j' ...
        + log(xgrid).^3*temp_m.del3_j' + log(xgrid).^4*temp_m.del4_j';
elseif islog == 2
    sp.spfun = @(xgrid) repmat(temp_m.alp_j',length(xgrid),1) + log(1+xgrid)*temp_m.del_j' + log(1+xgrid).^2*temp_m.del2_j' ...
        + log(1+xgrid).^3*temp_m.del3_j' + log(1+xgrid).^4*temp_m.del4_j';
else
    sp.spfun = @(xgrid) repmat(temp_m.alp_j',length(xgrid),1) + (xgrid)*temp_m.del_j' + (xgrid).^2*temp_m.del2_j' ...
        + (xgrid).^3*temp_m.del3_j' + (xgrid).^4*temp_m.del4_j';
end

% spatial function as a time
if islog == 1
    sp.spfun_t = @(xgrid,t) repmat(temp_m.alp_jt_mat2(:,t)',length(xgrid),1) + log(xgrid)*temp_m.del_j' + log(xgrid).^2*temp_m.del2_j' ...
        + log(xgrid).^3*temp_m.del3_j' + log(xgrid).^4*temp_m.del4_j';
elseif islog == 2
    sp.spfun_t = @(xgrid,t) repmat(temp_m.alp_jt_mat2(:,t)',length(xgrid),1) + log(1+xgrid)*temp_m.del_j' + log(1+xgrid).^2*temp_m.del2_j' ...
        + log(1+xgrid).^3*temp_m.del3_j' + log(1+xgrid).^4*temp_m.del4_j';
else
    sp.spfun_t = @(xgrid,t) repmat(temp_m.alp_jt_mat2(:,t)',length(xgrid),1) + (xgrid)*temp_m.del_j' + (xgrid).^2*temp_m.del2_j' ...
        + (xgrid).^3*temp_m.del3_j' + (xgrid).^4*temp_m.del4_j';
end

% spatial function as a time-city hall
if isfield(temp_m,'ejp')
    
    if isfield(temp_m, 'ejp0');
        alp_ch = temp_m.ejp0;
    else
        alp_ch = zeros(ncity,3);
    end
    
    if isfield(temp_m, 'ejp1');
        del_ch = temp_m.ejp1;
    else
        del_ch = zeros(ncity,3);
    end
    
    if isfield(temp_m, 'ejp2');
        del2_ch = temp_m.ejp2;
    else
        del2_ch = zeros(ncity,3);
    end
    
    if isfield(temp_m, 'ejp3');
        del3_ch = temp_m.ejp3;
    else
        del3_ch = zeros(ncity,3);
    end
    
    if isfield(temp_m, 'ejp4');
        del4_ch = temp_m.ejp4;
    else
        del4_ch = zeros(ncity,3);
    end
    
    
    if islog == 1
        sp.spfun_t_ch = @(xgrid,t,d) repmat(alp_ch(:,d)' + temp_m.alp_jt_mat2(:,t)',length(xgrid),1) ...
            + log(xgrid)*(del_ch(:,d)+temp_m.del_j)' + log(xgrid).^2*(del2_ch(:,d)+temp_m.del2_j)' ...
            + log(xgrid).^3*(del3_ch(:,d)+temp_m.del3_j)' + log(xgrid).^4*(del4_ch(:,d)+temp_m.del4_j)';
    elseif islog == 2
        sp.spfun_t_ch = @(xgrid,t,d) repmat(alp_ch(:,d)' + temp_m.alp_jt_mat2(:,t)',length(xgrid),1) ...
            + log(1+xgrid)*(del_ch(:,d)+temp_m.del_j)' + log(1+xgrid).^2*(del2_ch(:,d)+temp_m.del2_j)' ...
            + log(1+xgrid).^3*(del3_ch(:,d)+temp_m.del3_j)' + log(1+xgrid).^4*(del4_ch(:,d)+temp_m.del4_j)';
    else % level
        
        % d is city hall index
        sp.spfun_t_ch = @(xgrid,t,d) repmat(alp_ch(:,d)' + temp_m.alp_jt_mat2(:,t)',length(xgrid),1) ...
            + (xgrid)*(del_ch(:,d)+temp_m.del_j)' + (xgrid).^2*(del2_ch(:,d)+temp_m.del2_j)' ...
            + (xgrid).^3*(del3_ch(:,d)+temp_m.del3_j)' + (xgrid).^4*(del4_ch(:,d)+temp_m.del4_j)';
    end
    
    
    
end

